﻿var g_IsOpenAjax = true; //是否开启ajax 遮罩 变量
$(document).ready(function () {
    //初始化左边菜单
    $("#accordion dt").click(function () {
        $(this).toggleClass("on");
        $("." + $(this).attr("id") + "").slideToggle("0", function (e) {
        });
    });

    //    //左边菜单选中事件
    var currentPage = GetFile(jQuery.url.attr("path"));
    $("#accordion dd").each(function (i, n) {
        var item = GetFile(n.children[0].toString());
        var ids = "#accordion #dd" + n.className.toString().substring(2, n.className.toString().length);
        if (currentPage == item) {
            $(ids).trigger("click");
        }
    });


    LoadTopMenu();
    //    //菜单选中颜色
    //    var lastA = jQuery.url.param("html");
  
    if (currentPage) {
        var currentPageID = GetCurentFileID(currentPage);
       // $("#" + currentPage).css("color", "#f60");   
        $("#" + currentPageID).css("color", "#f60");
    }


    $('#loadings').ajaxStart(function () {
        loadStart();
    });
    //AJAX完成时关闭进度
    $('#loadings').ajaxComplete(function () {
        loadEnd();
    });
    //Ajax错误时关闭进度
    $('#loadings').ajaxError(function (result) {
        loadEnd();
        loadError(result);
    });
    //关闭全局Ajax缓存,全局超时时间为6分
    $.ajaxSetup({ cache: false, timeout: 6000000 });

    //刷新Session
   // KeepConnection();
});

//Ajax加载进度条开始
function loadStart() {
    try {

        //不开启ajax 遮罩模式
        if (!g_IsOpenAjax)
        {
            return;
        }

        //方式1
        $("#loadings").show();
        $("#loadings").addClass("ui-widget-overlay");
        $("#loadings").css("cursor", "wait");
        $("#loadings").width($("body")[0].offsetWidth);
        $("#loadings").height($("body")[0].clientHeight + window.screen.availHeight);
        $("#loadingimg").show();
        $("#loadingimg").css("top", window.screen.height / 2 - 100);
        $("#loadingimg").css("left", ($("body")[0].clientWidth / 2 - 50));
        $("body").css("overflow-x", "hidden");
        $("body").css("overflow-y", "hidden");

        //方式2
        /*
        $("#loadings").show();
        $("#loadings").css("cursor", "wait");
        $("#loadings").width(window.screen.width);
        $("#loadings").height($("body")[0].clientHeight + window.screen.availHeight);
        $("#loadingimg").show();
        $("#loadingimg").css("top", window.screen.height / 2 - $("#loadingimg").width());
        $("#loadingimg").css("left", ($("body")[0].clientWidth / 2 - $("#loadingimg").height()));
        $("body").css("overflow-x", "hidden");
        $("body").css("overflow-y", "hidden");
        */
    } catch (e) {
        alert("进度加载错误,\n 原因:" + e.message);
    }
    var basicVersionMenu = $("#BasicVersionMenu");
}
//Ajax加载进度条结束
function loadEnd() {
    setTimeout(_loadEnd, 500);
}
function _loadEnd() {
    //不开启ajax 遮罩模式
    if (!g_IsOpenAjax) {
        return;
    }
    $("#loadings").hide();
    $("#loadings").css("cursor", "default");
    $("body").css("overflow-x", "auto");
    $("body").css("overflow-y", "auto");
    $("#loadingimg").hide();
}
function loadError(result) {
    alert("操作失败,请稍后重试或联系管理员！");
}

//从链接中获取文件名 从 http://www.XX.com:port/controllerName/action,获取/controllerName/action
function GetFile(str) {
    if (str != "") {
        var temp = str;
        //var IndexOf = str.lastIndexOf('/');
        //var fileName = str.substring(IndexOf + 1, str.length);
        var IndexOf = temp.lastIndexOf('/');
        temp = temp.substring(0, IndexOf-1);
        var IndexOf2 = temp.lastIndexOf('/');  //倒数第2个斜杠位置
        var fileName = str.substring(IndexOf2, str.length);


        return fileName;
    }
    return "";
}

function GetCurentFileID(str)
{
    if (str != "") {
       
        var fileName = "";
        for(var i=0;i<str.length;i++)
        {
            if( str.charAt(i)=="/")
            {
                fileName+="_";
            }
            else
            {
                fileName+=str.charAt(i);
            }
        }
        return fileName;
    }
    return "";
}

function LoadTopMenu() {
    var cssRule;
    var newSelector;
    var styleRules;
    try {
        //IE6兼容性
        if ($.browser.msie) {
            for (var i = 0; i < document.styleSheets.length; i++)
                for (var x = 0; x < document.styleSheets[i].rules.length; x++) {
                    cssRule = document.styleSheets[i].rules[x];
                    if (cssRule.selectorText.indexOf("LI:hover") != -1) {
                        newSelector = cssRule.selectorText.replace(/LI:hover/gi, "LI.iehover");
                        document.styleSheets[i].addRule(newSelector, cssRule.style.cssText);
                    }
                }
        }
        var getElm = document.getElementById("nav").getElementsByTagName("LI");
        for (var i = 0; i < getElm.length; i++) {
            getElm[i].onmouseover = function () {
                this.className += " iehover";
                //IE6样式处理接口
                //this.className += " on";
            }
            getElm[i].onmouseout = function () {
                this.className = this.className.replace(new RegExp(" iehover\\b"), "");
                //IE6样式处理接口
                // this.className -= " on";
            }
        }
    }
    catch (Error)
    { }
}


function XSSCheck(Tvalue) {
    Tvalue = Tvalue.toLocaleLowerCase();
    var pt = ["<script", "<\/script", "alert", "<s", "<S", "javascript", "java", "script", "|", "&"];

    for (var i = 0; i < pt.length; i++) {
        if (Tvalue.indexOf(pt[i]) != -1) {
            alert("请勿输入非法字符。");
            return false;
        }
    }
    return true;
}


ValidStr = {
    VerifyInput: function (str) {//将数字与字母结合运用
        strUserID = str;
        for (nIndex = 0; nIndex < strUserID.length; nIndex++) {//通过循环判断字符串中的每个字符
            cCheck = strUserID.charAt(nIndex);

            if (!(ValidStr.IsDigit(cCheck) || ValidStr.IsAlpha(cCheck)) || cCheck == '-' || cCheck == '_') {
                return true;
            }
        }
        return false;
    },
    DigitInput: function (str) {
        strUserID = str;
        for (nIndex = 0; nIndex < strUserID.length; nIndex++) {//通过循环判断字符串中的每个字符
            cCheck = strUserID.charAt(nIndex);

            if (!(ValidStr.IsDigit(cCheck)) || cCheck == '-' || cCheck == '_') {
                return true;
            }
        }
        return false;
    },
    ChinaInput: function (str) {
        strUserID = str;
        for (nIndex = 0; nIndex < strUserID.length; nIndex++) {//通过循环判断字符串中的每个字符
            cCheck = strUserID.charAt(nIndex);

            if (!(ValidStr.IsChina(cCheck)) || cCheck == '-' || cCheck == '_') {
                return true;
            }
        }
        return false;
    },
    IsDigit: function (cCheck) //判断是否是数字
    { return (('0' <= cCheck) && (cCheck <= '9')); },
    IsAlpha: function (cCheck)//判断是否是字母
    { return ((('a' <= cCheck) && (cCheck <= 'z')) || (('A' <= cCheck) && (cCheck <= 'Z'))); },
    IsChina: function (cCheck) {
        return /.*[\u4e00-\u9fa5]+.*$/.test(cCheck);
    }
};

//function KeepConnection() {
//    $("#keepconnection").attr("src", "/User/KeepConnection");
//    setTimeout(KeepConnection, 300000);
//}

function LoadUserInfo() {

    var data = MakeParameters('UserController', 'Checklogin');
    Jsonhandler.AjaxPost(data, function (retResult) {
        document.getElementById("UserInfo").innerHTML = "用户：" + result['UserName'] + "[<a href=\"../User/logout?UId=" + result['UId'] + "\">登出</a>] ";
    });

}

///判断是否输入注入代码
function IsInjectCode(contents) {
    var Injects = "<br>@</br>";
    var arrInject = Injects.split('@');
    for (i = 0; i < arrInject.length; i++) {
        if (contents.indexOf(arrInject[i]) > -1) {
            return true;
        }
    }
    return false;
}